//
//  TLTweetDetailViewFrame.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/24.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLTweetDetailViewFrame.h"
#import "TLTweetPhotosView.h"
#import "TLTweet.h"
#import "TLOwner.h"
#import "TLHtmlMedia.h"

@implementation TLTweetDetailViewFrame

- (void)setTweet:(TLTweet *)tweet
{
    _tweet = tweet;
    
    // 1. 头像
    CGFloat iconX = 15.0;
    CGFloat iconY = 20.0;
    CGFloat iconW = 45.0;
    CGFloat iconH = 45.0;
    self.iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 2. 昵称
    CGFloat nameX = CGRectGetMaxX(self.iconFrame) + 20.0;
    CGFloat nameY = 25.0;
    self.nameFrame = CGRectMake(nameX, nameY, TLScreenW - 40, 20);
    
    // 3. 时间
    CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(self.nameFrame) + 5;
    self.timeFrame = CGRectMake(timeX, timeY, 100, 20);
    
    // 4. 内容
    CGFloat h = 0;
    CGFloat locationY = 0;
    CGFloat contentX = iconX;
    CGFloat contentY = CGRectGetMaxY(self.timeFrame) + 15;
    CGSize resultSize = CGSizeZero;
    if (tweet.content.length <= 0) {
        tweet.content = @"  ";
    }
    CGSize maxSize = CGSizeMake(TLScreenW - contentX * 2, 200);
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    TLHtmlMedia *htmlMedia = [tweet htmlMedia];
    tweet.content = htmlMedia.contentDisplay;
    tweet.htmlMedia = htmlMedia;
    resultSize = [tweet.content boundingRectWithSize:CGSizeMake(maxSize.width, maxSize.height)
                options:(NSStringDrawingUsesLineFragmentOrigin)
                                 attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], NSParagraphStyleAttributeName: style}
                                    context:nil].size;
    if(resultSize.height > 200){
        resultSize.height = 200;
    }
    self.contentFrame = CGRectMake(contentX, contentY, maxSize.width, resultSize.height);
    h = CGRectGetMaxY(self.contentFrame) + 15;
    if(tweet.htmlMedia.imageItems.count > 0){
       
        TLTweetPhotosView *tweetPhotosView = [[TLTweetPhotosView alloc] init];
        CGSize photosViewSize = [tweetPhotosView sizeWithHtmlMediaImageCount:tweet.htmlMedia.imageItems];
        self.photosFrame = CGRectMake(contentX, CGRectGetMaxY(self.contentFrame) + 10, photosViewSize.width, photosViewSize.height);
        h = CGRectGetMaxY(self.photosFrame) + 15;
        locationY = CGRectGetMaxY(self.photosFrame) + 20;
    }else{
        
        locationY = CGRectGetMaxY(self.contentFrame) + 20;
    }
    
    if(tweet.location.length > 0){
        
        self.locationLabelFrame = CGRectMake(contentX, locationY, 100, 12);
        if(tweet.device.length > 0){
            
            self.fromIconFrame = CGRectMake(self.locationLabelFrame.origin.x, CGRectGetMaxY(self.locationLabelFrame) + 10, 20, 20);
            h = CGRectGetMaxY(self.fromIconFrame) + 15;
        }else{
            
            h = CGRectGetMaxY(self.locationLabelFrame) + 15;
        }
    }else{
        
        if(tweet.device.length > 0){
            CGFloat fromIconY = 0;
            if(tweet.htmlMedia.imageItems.count > 0){
                
                fromIconY = CGRectGetMaxY(self.photosFrame) + 20;
            }else{
                
                fromIconY = CGRectGetMaxY(self.contentFrame) + 20;
            }
            self.fromIconFrame = CGRectMake(self.locationLabelFrame.origin.x, fromIconY, 20, 20);
            h = CGRectGetMaxY(self.fromIconFrame) + 15;
        }else{
            
            if(tweet.htmlMedia.imageItems.count > 0){
                
                h  = CGRectGetMaxY(self.photosFrame) + 15;
            }else{
                
                h = CGRectGetMaxY(self.contentFrame) + 15;
            }
        }
    }

    self.frame = CGRectMake(0, 0, TLScreenW, h);
}

@end
